import os
import tkinter

from config import BASC
from def_pack.doc_handle import DocHandle
from def_pack.frame_add_str import add_str, add_list_num, add_dict_num
from def_pack.normal_solve import get_show_list, count_per_row_str_inlist, num_peop_to_dict, se_every_file_in_normal



# def on_select(event):
#     print(event)
#
#     if event.select_clear():
#         # event.widget.select_clear()  # clear selected text.
#         event.select_clear()  # clear selected text.
#     return "break"

def get_frame_program2(beginstr,filename,cls=0,tetype=0):
    # docxfile=f"{BASC}/word文档/{docxname}.docx"
    tetype=1
    #这里一般是从头开始，所以要设置好值
    listaa=get_show_list(beginstr,filename,tetype)
    line_numstr=count_per_row_str_inlist(listaa)
    # clsdoc=DocHandle(docxfile)
    if cls==0:
        main_win = tkinter.Tk()
    # .Toplevel()
    else:
        main_win = tkinter.Toplevel()
    #设置图标ico
    main_win.iconbitmap(f'{BASC}/icon/Text-Docs.ico')

    # main_win.geometry("560x800")
    main_win.geometry("605x770")
    #调整窗口在屏幕上的位置
    # main_win.geometry("+730+156")
    main_win.geometry("+400+50")

    pl1=str(filename).rfind(".")
    pl2=str(filename).rfind("/")+1
    title_pl=str(filename)[pl2:pl1]
    # main_win.title("扒小说")
    main_win.title(f"{title_pl}")

    # tkinter.Text(main_win, width=35, height=15)

    # textframe=tkinter.Text(main_win,setgrid=True)
    textframe=tkinter.Text(main_win,exportselection=False,fg='#2a2a2a',bg='white')
    scrollbar_v = tkinter.Scrollbar(main_win)
    # scrollbar_v.pack(side="right", fill="y")
    scrollbar_v.pack(side=tkinter.RIGHT,fill=tkinter.Y)

    # 两个控件关联
    scrollbar_v.config(command=textframe.yview)
    textframe.config(yscrollcommand=scrollbar_v.set)


    textframe.pack(side="left",fill="both",expand=True)
    peodict=num_peop_to_dict(listaa)
    # add_str(listaa,textframe,clsdoc)
    add_list_num(listaa, textframe, line_numstr)
    # add_dict_num(peodict, textframe, line_numstr)


    # textframe.bindtags((str(textframe), "main_win", "PostEvent", ".", "all"))
    # textframe.bind_class("PostEvent", "<ButtonPress-1><Motion>", on_select)

    # 这是增加一个阻止函数，禁止选择的。
    # textframe.bindtags((str(textframe), "Text", "PostEvent", ".", "all"))
    #
    # textframe.bind_class("PostEvent", "<Button1-Motion>", on_select)

    #这里要放在最后面，不然写入不了文字
    #这个要同时放在文本框和里面的标签里面，不然会造成差异，也就是不行的。
    textframe.config(state="disabled",selectbackground="white",foreground="Black")
    # textframe.config(state="disabled")
    # textframe.bindtags((str(textframe), str('<main_win-Motion>'), "all"))
    # print(main_win.bindtags())
    # os.system("pause")
    # '<Button1-Motion>'
    # i = textframe.bind("main_win",'<Button1-Motion>', int)
    # i = main_win.bind('<Button1-Motion>')
    # i = textframe.bind('<Button-1>')
    # textframe.unbind('<Button1-1>',i)
    # print(i)
    main_win.mainloop()

#这是多个替换文件的
def more_frame_occur(dirpath):
    filepathlist=se_every_file_in_normal(dirpath)
    for kk in filepathlist:
        get_frame_program2(" ", kk, BASC)

if __name__ == '__main__':

    filename = f"{BASC}/"
    beginstr = ""

    # docxname = "筛选关键字的"
    # line_numstr=
    get_frame_program2(beginstr, filename)
